package com.eastedu.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eastedu.common.util.SchoolGroupMappingUtil;
import com.eastedu.repository.basic.mapper.BasicResourceMapper;
import com.eastedu.repository.basic.po.BasicResourcePO;
import com.eastedu.service.AbstractDataSyncService;
import com.eastedu.service.DataSourceService;
import org.springframework.stereotype.Service;


/**
 * 〈〉
 *
 * @author ZHISHUAI.ZHANG
 * @since 2021 /7/28
 */
@Service("basic-source")
@DS("basic")
public class BasicDataSourceServiceImpl extends AbstractDataSyncService<BasicResourcePO> implements DataSourceService {

    /**
     * Instantiates a new Basic data source service.
     *
     * @param mapper the mapper
     */
    public BasicDataSourceServiceImpl(BasicResourceMapper mapper) {
        super(mapper);
        super.addScriptByTableName("t_basic_resource");
        //        super.addScript("UPDATE t_basic_resource t1 SET t1.sync_state = 0 \n" +
        //                "WHERE school_group_id IS NULL OR school_group_id = 0");
    }

    @Override
    protected void updateContent(BasicResourcePO po) {
        po.setSchoolGroupId(SchoolGroupMappingUtil.mappingId(po.getSourceOrgId()));
    }

    @Override
    public LambdaQueryWrapper<BasicResourcePO> getExample() {
        return Wrappers.lambdaQuery(this.getClazz())
                .eq(BasicResourcePO::getSyncState, 0)
                .in(BasicResourcePO::getSourceAppCode, "cHJlcGFyaW5nLWxlc3Nvbg==")
                .orderByAsc(BasicResourcePO::getId);
    }
}